package collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

/**
 * ArrayList的使用
 * 存储结构：数组，查找遍历速度快，增删慢
 * @author  zff
 *
 */
public class Demo05 {
    public static void main(String[] args) {
        //创建集合
        ArrayList<Object> arrayList = new ArrayList<>();
        //1,添加元素
        Student s1 = new Student("刘德华", 22);
        Student s2 = new Student("郭富城", 24);
        Student s3 = new Student("梁朝伟", 23);
        Student s4 = new Student("黎明", 23);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s4);
        System.out.println("元素个数：" + arrayList.size());
        System.out.println(arrayList.toString());
        //2.删除操作 重写equals方法
        arrayList.remove(new Student("刘德华",20));//equals(this == obj)
        arrayList.remove(s1);
        System.out.println("删除之后：" + arrayList.size());
        //3.1遍历元素
        System.out.println("=====使用for循环进行遍历=====");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
        Iterator<Object> it = arrayList.iterator();
        while(it.hasNext()){
            Student s = (Student) it.next();
            System.out.println(s.toString());
        }
        //3.2列表迭代器
        System.out.println("-------使用列表迭代器顺序遍历元素------");
        ListIterator<Object> lit= arrayList.listIterator();
        while(lit.hasNext()){
            System.out.println(lit.nextIndex() + " :" + lit.next()) ;
        }
        System.out.println("-------使用列表迭代器逆序遍历元素------");
        while(lit.hasPrevious()){
            System.out.println(lit.previousIndex() + " :" + lit.previous());
        }
        //4.判断
        System.out.println(arrayList.contains(new Student("刘德华", 22)));
        System.out.println(arrayList.isEmpty());
        //5.查找
        System.out.println(arrayList.indexOf(new Student("黎明", 23)));

    }
}
